/* Turska UI library
 * Copyright 2003-2008 Jetro Lauha
 * All rights reserved.
 * Web: http://iki.fi/jetro/
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of EITHER:
 *   (1) The GNU Lesser General Public License as published by the Free
 *       Software Foundation; either version 2.1 of the License, or (at
 *       your option) any later version. The text of the GNU Lesser
 *       General Public License is included with this library in the
 *       file LICENSE-LGPL.txt.
 *   (2) The BSD-style license that is included with this library in
 *       the file LICENSE-BSD.txt.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the files
 * LICENSE-LGPL.txt and LICENSE-BSD.txt for more details.
 *
 * $Id: UICheckBox.cpp,v 1.2 2008/03/27 11:44:26 tonic Exp $
 * $Revision: 1.2 $
 */

#include <assert.h>
#include "SDL.h"
#include "SDL_opengl.h"
#include "UI.h"


UICheckBox::UICheckBox(INT x, INT y, UIImage *defaultImage, UIImage *pressedImage,
                       UIImage *checkImage, UIFont *font, const char *text,
                       bool checkedState, UINT textAnchor, UINT32 fontFlags,
                       INT textX, INT textY) :
    UIButton(x, y, defaultImage, pressedImage, checkImage,
             font, text, fontFlags, textX, textY, 0, 0),
    mIsChecked(checkedState),
    mAnchor(textAnchor), mStringWidth(0)
{
    UIImage *sizeImage = NULL;
    INT width = 0, height = 0;

    if (mDefaultImage != NULL)
        sizeImage = mDefaultImage;
    else if (mPressedImage != NULL)
        sizeImage = mPressedImage;
    else if (mSymbolImage != NULL)
        sizeImage = mSymbolImage;
 
    if (sizeImage != NULL)
    {
        width += sizeImage->getWidth();
        height += sizeImage->getHeight();
    }

    mCheckX = width / 2;

    const UINT gapSize = 4; // TODO - Fix the hardcoded gap size to a customizable one.

    // note: mTextX & mTextY has already been determined by UIButton constructor
    if (textX == -1)
    {
        switch (textAnchor)
        {
        case ANCHOR_LEFT:
            mTextX = -gapSize;
            break;
        case ANCHOR_MIDDLE:
            mTextX = width / 2;
            break;
        case ANCHOR_RIGHT:
            mTextX = width + gapSize;
            break;
        }
    }
    if (textY == -1)
        mTextY = height / 2;

    if (mText != NULL && mFont != NULL)
    {
        mStringWidth = mFont->getStringWidth(mText);
        switch (textAnchor)
        {
        case ANCHOR_LEFT:
        case ANCHOR_RIGHT:
            width += mStringWidth + gapSize;
            break;
        case ANCHOR_MIDDLE:
            if (mStringWidth > width)
                width = mStringWidth;
            break;
        }
        UINT fontHeight = mFont->getFontHeight();
        if (fontHeight > height)
            height = (INT)fontHeight;
    }

    mCheckY = height / 2;

    setWidth(width);
    setHeight(height);
}


bool UICheckBox::isInside(INT x, INT y)
{
    switch (mAnchor)
    {
    case ANCHOR_LEFT:
        return x >= mX + mTextX - mStringWidth && y >= mY &&
               x < mX + mTextX - mStringWidth + mWidth && y < mY + mHeight;
    case ANCHOR_RIGHT:
        return x >= mX && y >= mY && x < mX + mWidth && y < mY + mHeight;
    case ANCHOR_MIDDLE:
        return x >= (mX + mCheckX - mWidth / 2) && y >= mY &&
               x < (mX + mCheckX + mWidth / 2) && y < mY + mHeight;
    }
    assert(0 && "Unknown text anchor for UICheckBox.");
    return false;
}


void UICheckBox::onPointerUp(INT x, INT y)
{
    if (isPressed() && isInside(x, y))
        mIsChecked = !mIsChecked;
    UIButton::onPointerUp(x, y);
}


void UICheckBox::draw()
{
    UIImage *image = mDefaultImage;

    if (isPressed() && mPressedImage != NULL)
        image = mPressedImage;

    if (image != NULL)
        image->draw(mImageDrawMode, mImageFlags, mX, mY, mColor);

    if (mText != NULL)
        mFont->drawString(mX + mTextX, mY + mTextY, mFontFlags, mText, mItemColor);

    if (mSymbolImage != NULL && isChecked())
        mSymbolImage->draw(mSymbolImageDrawMode, mSymbolImageFlags, mX + mCheckX, mY + mCheckY, mItemColor);
}
